<?php  
function getFile($url, $save_dir = '', $filename = '', $type = false) {  
    if (trim($url) == '') {  
        return false;  
    }  
    if (trim($save_dir) == '') {  
        $save_dir = './';  
    }  
    if (0 !== strrpos($save_dir, '/')) {  
        $save_dir.= '/';  
    }  
    //echo 'file:' .$save_dir.$filename. '\n';
    /*if (file_exists($save_dir.$filename))
    {
        file_put_contents($save_dir.$filename, "");
    }
    //创建保存目录  
    if (!file_exists($save_dir) && !mkdir($save_dir, 0777, true)) {  
        return false;  
    }  */
    //获取远程文件所采用的方法  
    if ($type == false) {  
        /*$ch = curl_init();  
        $timeout = 5;  
        curl_setopt($ch, CURLOPT_URL, $url);  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);  
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        $content = curl_exec($ch);  
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        var_dump($httpcode);
        var_dump($content);
        if (is_bool($content) && !$content)
        {
            echo 'failed..';
            return false;
        }
        if ($httpcode == '404')
        {
            echo 'failed:' .$httpcode;
            return false;
        }
        curl_close($ch);  */
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); 
        //curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
        curl_setopt($ch, CURLOPT_TIMEOUT, 3600);
        curl_setopt($ch, CURLOPT_BUFFERSIZE, 20971520);
        $flag=0;
        curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch ,$str) use (&$flag,$filename){
            $len = strlen($str);
            $flag++;
            $type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
            if($flag==1){
                $size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
                $type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
                $httpcode  = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                header("HTTP/1.1 ".$httpcode);
                header("Content-Type: ".$type);
                header("Content-Disposition:  attachment;  filename=".$filename);
                header("Content-Length: ".$size);
                header('Content-Transfer-Encoding: binary');
                //header('Cache-Control:max-age=2592000');
            }
            echo $str;
            return $len;
        });
        $output = curl_exec($ch);
        curl_close($ch);
        die;
    }
}  
 

if(is_array($_GET)&&count($_GET)>0)//先判断是否通过get传值了
{
    if(isset($_GET["f"]))//是否存在"id"的参数
    {
        $f=$_GET["f"];//存在
    }
}
if(empty($f))
{
    echo 'no param';
    die;
}
if($f[0] == '/')
{
    $f = substr($f, 1);
}
$url="http://mirfiles.co.uk/mir2/cmir/patch/".$f;
$save_dir = dirname($f);
$filename = basename($f);
$res = getFile($url, $save_dir, $filename,false);//0  1 都是好使的 
if (is_bool($res) && !$res)
{
    echo 'failed';
    die;
}
//var_dump($res);
/*header( "Content-Disposition:  attachment;  filename=".$filename); //告诉浏览器通过附件形式来处理文件
header('Content-Length: ' . filesize($f)); //下载文件大小
readfile($f);  //读取文件内容
file_put_contents($f, "");*/

?>